package Ajax;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

public class AttachmentPage extends AbsPersonalPage {

	public AttachmentPage(String id, HttpServletRequest request) {
		super(id, request);
		mStrSubClassName = "resumeattachment";
	}

	@Override
	public void execSql(Statement stmt, HttpServletRequest request)
			throws SQLException {
		if (bCreate) {
			return;
		}

		if (bSubmit) {
			String sql = "UPDATE resume SET pic_url = '"
					+ "888" + "', pic_size = '"
					+ "999" + "', message = '"
					+ "777" + "' WHERE id = " + mId;

			stmt.executeUpdate(sql);

			return;
		}

		if (bDelete) {
			String sql = "UPDATE resume SET pic_url = null, pic_size = null, message = null WHERE id = " + mId;

			stmt.executeUpdate(sql);

			return;
		}

		String sql = "SELECT pic_size, pic_url, message FROM resume WHERE id = " + mResumeId;

		rs = stmt.executeQuery(sql);
	}

	@Override
	protected void writeWithoutCreate(PrintWriter writer) throws SQLException {
		if (rs == null) {
			return;
		}

		writer.print("<ol><li>状态: 已审核, 该附件将在您的简历中显示.</li><li>状态: 未审核, 该附件不在您的简历中显示.</li>"
				+ "<li>您可上传的附件, 格式为: <span class=\"n\">jpg,gif</span> .</li><li>您可上传的单个附件最大尺寸为: <span class=\"n\">100</span> (K) .</li></ol>"
				+ "<div class=\"topic_list\">"
				+ "<table cellpadding=\"0\" cellspacing=\"0\">"
				+ "<thead>"
				+ "<tr><td>状态</td><td>附件名称</td><td>附件尺寸</td><td>管理选项</td></tr>"
				+ "</thead>");

		boolean has_pic = false;
		
		if (rs.next()) {
			String pic_url = rs.getString("pic_url");
			String pic_size = rs.getString("pic_size");

			if(pic_url != null && pic_size != null && pic_url.length() > 5 && pic_size.length() > 7) {
				has_pic = true;
				
				writer.println("<tr class=\"bg1\">");
				writer.println("<td><img src=\"./images/personal/property_y.gif\" alt=\"已审核\" /></td>");
				writer.println("<td><a href=\"./upload/0010/" + pic_url + "\" target=\"_blank\"><strong>简历照片</strong></td>");
				writer.println("<td>" + pic_size + "</td>");
	
				writer.println("<td><a href=\"#\" onclick=\"resumeattachment('','','"
						+ rs.getString("id") + "');return false;\">修改</a> | ");
				writer.println("<a href=\"#\" onclick=\"if (confirm('您是否确定要删除该条教育经历\t')){resumeattachment('','','','"
						+ rs.getString("id")
						+ "');return false;}else{return false;}\">删除</a></td>");
			}
		}

		writer.println("</tr></table></div><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\"><tr><th width=\"100\">&nbsp;</th>	<td>");
		
		if (!has_pic) {
			writer.println("<input type=\"button\" name=\"create\" value=\"添加照片\" onclick=\"resumeattachment('',true)\" class=\"submit\">");
		}

		writer.println("<input type=\"button\" name=\"nextstep\" value=\"跳到下一步\" onclick=\"resumeload('append')\" class=\"button\" />");
		writer.println("</td></tr></table><div id=\"resumextentform\"></div>");
	}

	@Override
	protected void writeWithCreate(PrintWriter writer) throws IOException {
		String form_title = "<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=attachment&amp;info=" + mResumeId + "&amp;submit=1\" ";
		File file = new File("c:\\xml\\attachment.xml");

		if (file.exists()) {
			InputStream fs = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fs,
					"utf-8"));

			String Line = null;

			writer.print(form_title);

			while ((Line = br.readLine()) != null) {
				writer.println(Line);
			}

			br.close();
			fs.close();
		} else {
			file.createNewFile();
		}
	}
}
